<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      v-show="showSearch"
      :inline="true"
    >
      <el-form-item label="用户名称" prop="useName">
        <el-input
          v-model="queryParams.useName"
          placeholder="请输入用户名称"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号码"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="openSelectUser(1)"
          >添加企业用户</el-button
        >
        <el-button type="primary" plain icon="Plus" @click="openSelectUser(2)"
          >添加个人用户</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="CircleClose"
          :disabled="multiple"
          @click="cancelAuthUserAll"
          v-hasPermi="['system:role:remove']"
          >批量取消授权</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Close" @click="handleClose"
          >关闭</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="handleQuery"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="userList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="用户id" prop="id" :show-overflow-tooltip="true" />
      <el-table-column
        label="用户名称"
        prop="name"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="手机"
        prop="mobile"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="企业/个人" align="center" prop="isBusiness">
        <template #default="scope">
          <span v-if="scope.row.isBusiness == 1">企业</span>
          <span v-else>个人</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="CircleClose"
            @click="cancelAuthUser(scope.row)"
            v-hasPermi="['system:role:remove']"
            >取消授权</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <select-enterprise
      ref="selectRef1"
      :roleId="roleId"
      @ok="handleQuery"
      :type="istype"
    />
    <select-user
      ref="selectRef"
      :roleId="roleId"
      @ok="handleQuery"
      :type="istype"
    />
  </div>
</template>

<script setup name="AuthUser">
import selectUser from "./selectUser";
import selectEnterprise from "./selectEnterprise";
import {
  allocatedUserList,
  authUserCancel,
  authUserCancelAll,
} from "@/api/system/role";

import {
  queryRoleDetailRequest,
  unAssignUserRequest,
} from "@/api/frontRole/frontRole";
import { onMounted } from "vue";
import { useRoute } from "vue-router";

const route = useRoute();
const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");

const userList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const multiple = ref(true);
const total = ref(0);
const userIds = ref([]);

const queryParams = reactive({
  useName: undefined,
  phone: undefined,
});

let roleId = ref("");
onMounted(() => {
  const id = route.query.id;
  roleId.value = id;
  getList(id);
});

/** 查询授权用户列表 */
function getList(id) {
  loading.value = true;

  queryRoleDetailRequest(id, queryParams).then((res) => {
    console.log(res);
    userList.value = res.data.users;
    loading.value = false;
  });
}
// 返回按钮
function handleClose() {
  const obj = { path: "/frontRole/frontRoleUser" };
  proxy.$tab.closeOpenPage(obj);
}
/** 搜索按钮操作 */
function handleQuery() {
  // queryParams.pageNum = 1;
  getList(roleId.value);
}
/** 重置按钮操作 */
function resetQuery() {
  queryParams.phone = "";
  queryParams.userName = "";
  proxy.resetForm("queryRef");
  handleQuery();
}
// 多选框选中数据
function handleSelectionChange(selection) {
  userIds.value = selection.map((item) => item.userId);
  multiple.value = !selection.length;
}

let istype = ref(1); //1企业 2个人
/** 打开授权用户表弹窗 */
function openSelectUser(type) {
  if (type == 1) {
    proxy.$refs["selectRef1"].show();
  } else {
    proxy.$refs["selectRef"].show();
  }
}
/** 取消授权按钮操作 */
function cancelAuthUser(row) {
  proxy.$modal
    .confirm('确认要取消该用户"' + row.name + '"角色吗？')
    .then(function () {
      return unAssignUserRequest({
        roleId: roleId.value,
        userId: row.id,
      });
    })
    .then(() => {
      getList(roleId.value);
      proxy.$modal.msgSuccess("取消授权成功");
    })
    .catch(() => {});
}
/** 批量取消授权按钮操作 */
function cancelAuthUserAll(row) {
  const roleId = queryParams.roleId;
  const uIds = userIds.value.join(",");
  proxy.$modal
    .confirm("是否取消选中用户授权数据项?")
    .then(function () {
      return authUserCancelAll({ roleId: roleId, userIds: uIds });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("取消授权成功");
    })
    .catch(() => {});
}
</script>
